package controller

import (
	"context"
	v1 "wh-1-backend/api/v1"
	"wh-1-backend/internal/model"
	"wh-1-backend/internal/service"
)

type cResume struct{}

var Resume = cResume{}

func (r *cResume) GetResumePageList(ctx context.Context, req *v1.GetResumePageListReq) (res *v1.GetResumePageListRes, err error) {
	total, list, err := service.Resume().ListByConditionWithPage(ctx, model.PageInput(req.PageCommonReq), model.ResumePageListCondition{
		Username: req.Username,
		Phone:    req.Phone,
	})
	if err == nil {
		res = &v1.GetResumePageListRes{
			Total: total,
			List:  list,
		}
	}
	return
}

func (r *cResume) CreateResume(ctx context.Context, req *v1.CreateResumeReq) (res *v1.Res, err error) {
	workExperience := make([]*model.EditWorkExperienceInput, 0, 8)
	for _, experienceReq := range req.WorkExperience {
		workExperience = append(workExperience, &model.EditWorkExperienceInput{
			Id:        0,
			ResumeId:  0,
			From:      experienceReq.From,
			End:       experienceReq.End,
			CompanyId: experienceReq.CompanyId,
			Remarks:   experienceReq.Remarks,
		})
	}
	projectExperience := make([]*model.EditProjectExperienceInput, 0, 8)
	for _, experienceReq := range req.ProjectExperience {
		projectExperience = append(projectExperience, &model.EditProjectExperienceInput{
			Id:       0,
			ResumeId: 0,
			From:     experienceReq.From,
			End:      experienceReq.End,
			Title:    experienceReq.Title,
			Content:  experienceReq.Content,
		})
	}
	err = service.Resume().SaveAndAttach(ctx, model.EditResume1Input{
		EditResumeInput:   model.EditResumeInput(req.EditResumeReq),
		WorkExperience:    workExperience,
		ProjectExperience: projectExperience,
	})
	return
}

func (r *cResume) ModifyResume(ctx context.Context, req *v1.ModifyResumeReq) (res *v1.Res, err error) {
	workExperience := make([]*model.EditWorkExperienceInput, 0, 8)
	for _, experienceReq := range req.WorkExperience {
		workExperience = append(workExperience, &model.EditWorkExperienceInput{
			Id:        0,
			ResumeId:  req.Id,
			From:      experienceReq.From,
			End:       experienceReq.End,
			CompanyId: experienceReq.CompanyId,
			Remarks:   experienceReq.Remarks,
		})
	}
	projectExperience := make([]*model.EditProjectExperienceInput, 0, 8)
	for _, experienceReq := range req.ProjectExperience {
		projectExperience = append(projectExperience, &model.EditProjectExperienceInput{
			Id:       0,
			ResumeId: req.Id,
			From:     experienceReq.From,
			End:      experienceReq.End,
			Title:    experienceReq.Title,
			Content:  experienceReq.Content,
		})
	}
	err = service.Resume().UpdateAndAttach(ctx, req.Id, model.EditResume1Input{
		EditResumeInput:   model.EditResumeInput(req.EditResumeReq),
		WorkExperience:    workExperience,
		ProjectExperience: projectExperience,
	})
	return
}

func (r *cResume) GetResumeDetail(ctx context.Context, req *v1.GetResumeDetailReq) (res *v1.GetResumeDetailRes, err error) {
	detail, err := service.Resume().DetailById(ctx, req.Id)
	res = &v1.GetResumeDetailRes{
		ResumeDetail1: detail,
	}
	return
}

func (r *cResume) DeleteResume(ctx context.Context, req *v1.DeleteResumeReq) (res *v1.Res, err error) {
	err = service.Resume().Delete(ctx, req.Ids)
	return
}
